package com.smcdd.cdb.dbo.cdb_dbo.service.presenter;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.smcdd.cdb.dbo.cdb_dbo.app.App;
import com.smcdd.cdb.dbo.cdb_dbo.network.HttpObserver;
import com.smcdd.cdb.dbo.cdb_dbo.network.factory.RetrofitFactory;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.BaseResponseEntity;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Simple;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.DataManager;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.NetWorkCodeInfo;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.ISelectShopAc;
import com.smcdd.cdb.dbo.cdb_dbo.ui.activity.SelectShopActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.activity.StatisticsActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.adapter.SelectAdapter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BasePresenter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainEncry;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainVersion;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 作者：Administrator on 2017/7/1 12:12
 * 邮箱：zhanghuaiha@gmail.com
 */

public class SelectShopPresenter extends BasePresenter<ISelectShopAc> {
    private String udid;
    private SelectShopActivity selectShopActivity;
    private DataManager manager;
    private String versionCode;
    private String eid;
    private static final String TAG = SelectShopPresenter.class.getSimpleName();
    private SelectAdapter selectAdapter;
    private RecyclerView recycler;
    private LinearLayoutManager linearLayoutManager;
    private List<Simple> data;
    private String total;
    private int cont = 1;


    public SelectShopPresenter(BaseActivity context) {
        super(context);
    }

    public void loadContacts(String udid){
        this.udid = udid;
        selectShopActivity = getView().getAc();
        recycler = getView().getRv();
        linearLayoutManager = new LinearLayoutManager(mContext);
        recycler.setLayoutManager(linearLayoutManager);
        manager = new DataManager(mContext);
        versionCode = ObtainVersion.getVersionName(mContext);
        eid = App.SP.getString("id", "");
        setAdapter();
        loadNetWork(udid);

        //加载更多
        recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    int lastVisiblePosition = linearLayoutManager.findLastVisibleItemPosition();
                    if (lastVisiblePosition >= linearLayoutManager.getItemCount() - 1) {
                        Log.e("layoutManager", "一定到底部了 准备加载更多");
                        if (total != null) {
                            if (Integer.parseInt(total) < 20) {
                                return;
                            } else {
                                String time = System.currentTimeMillis() + "";
                                String encry = ObtainEncry.getStoreInfo("2", time, versionCode, NetWorkCodeInfo.SIMPLE_PAGE, udid, eid, "20", cont+"");
                                getDate(udid,time,encry,cont+"");
                                cont++;
                            }
                        }
                    }
                }
            }
        });


    }


    private void setAdapter() {
        selectAdapter = new SelectAdapter(mContext);
        recycler.setAdapter(selectAdapter);
        selectAdapter.setItemClickListenter((scan, position) -> {
            Intent intent = new Intent(selectShopActivity, StatisticsActivity.class);
            intent.putExtra("id",scan.getId());
            intent.putExtra("name",scan.getName());
            selectShopActivity.jumpToActivity(intent);
        });
    }

    private void loadNetWork(String udid) {
        String time = System.currentTimeMillis() + "";
        String encry = ObtainEncry.getStoreInfo("2", time, versionCode, NetWorkCodeInfo.SIMPLE_PAGE, udid, eid, "20", "0");
        getDate(udid,time,encry,"0");
    }

    public void getDate(String udid,String time,String encry,String count){
        RetrofitFactory.getmInstanse().mApi.getSimple("2", time, versionCode, NetWorkCodeInfo.SIMPLE_PAGE, udid, eid, "20", count,encry)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseResponseEntity<List<Simple>>>() {
                    @Override
                    public void success(BaseResponseEntity<List<Simple>> simpleBaseResponseEntity) {
                        if (simpleBaseResponseEntity.code == 200) {
                            total = simpleBaseResponseEntity.total;
                            data = simpleBaseResponseEntity.data;
                            if (count.equals("0")) {
                                selectAdapter.addData(data);
                            }else {
                                selectAdapter.addLastData(data);
                            }
                        }else {
                            ToastUtil.showToast(simpleBaseResponseEntity.message);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        e.printStackTrace();
                    }
                });
    }
}
